package org.comix.core;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * 
 * @Creator paladin
 * @Created-date 2009-1-15
 */
public class NestedRuntimeException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5506726887801670270L;

	protected String msg = null;

	protected Throwable cause = null;

	public NestedRuntimeException() {
		super();

	}

	public NestedRuntimeException(String message, Throwable cause) {
		super(message, cause);
	}

	public NestedRuntimeException(String message) {
		super(message);
	}

	public NestedRuntimeException(Throwable cause) {
		super(cause);
	}

	public Throwable getCause() {
		return cause;
	}

	public String getMessage() {
		StringBuffer sb = new StringBuffer();
		String msg = super.getMessage();

		if (msg != null) {
			sb.append(msg);

			if (cause != null) {
				sb.append(": ");
			}
		}

		if (cause != null) {
			sb.append("root cause: " + ((cause.getMessage() == null) ? cause.toString() : cause.getMessage()));
		}

		return sb.toString();
	}

	public Throwable getcause() {
		return cause;
	}

	public void printStackTrace() {
		super.printStackTrace();

		if (cause != null) {
			synchronized (System.err) {
				System.err.println("\n");
				cause.printStackTrace();
			}
		}
	}

	public void printStackTrace(PrintStream s) {
		super.printStackTrace(s);

		if (cause != null) {
			synchronized (s) {
				s.println("\n");
				cause.printStackTrace(s);
			}
		}
	}

	public void printStackTrace(PrintWriter s) {
		super.printStackTrace(s);

		if (cause != null) {
			synchronized (s) {
				s.println("\n");
				cause.printStackTrace(s);
			}
		}
	}

}
